/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlet;

import DataAccess.BDocAccess;
import Entity.BDoc;
import Entity.ODoc;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author pdaitoan
 */
public class BDocServet extends HttpServlet {

    BDocAccess access;
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, ParseException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            access = new BDocAccess();
            String date_str = request.getParameter("txtDraftDate");
            SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            String action = request.getParameter("action");
            out.println(action);
            if (action.equals("add"))
            {
                String button = "";
                button = request.getParameter("btnSave");
                // if button Save is clicked
                if (button != null)
                {
                    // Add new IDoc
                    
                    Date DraftDate = df.parse(date_str);

                    date_str = request.getParameter("txtSignDate");
                    Date SignDate = df.parse(date_str);

                    BDoc bdoc = new BDoc();
                    bdoc.setDocNum(request.getParameter("txtDocNum"));
                    bdoc.setSignDate(SignDate);
                    bdoc.setSignByName(request.getParameter("txtSignByName"));
                    bdoc.setReplyReceiveName(request.getParameter("txtReplyReceiveName"));
                    bdoc.setDraftUserName(request.getParameter("txtDraftUsername"));
                    bdoc.setDraftDate(DraftDate);
                    bdoc.setState(Boolean.FALSE); // Chua Approve
                    bdoc.setSubject(request.getParameter("txtSubject"));
                    bdoc.setBriefContent(request.getParameter("txtBriefContent"));
                    bdoc.setNotes(request.getParameter("txtNotes"));
                    bdoc.setDocumentName(request.getParameter("fname"));
                    Long id = Long.parseLong(request.getParameter("cbxAccount"));
                    bdoc.setAccountID(id);

                    if (access.addBDoc(bdoc))
                        response.sendRedirect("VanBanNoiBo/DisplayBDoc.jsp");
                    else
                        response.sendRedirect("VanBanNoiBo/CreateBDoc.jsp");
                }
            }
            else if(action.equals("edit"))
            {
                
                String button = "";
                button = request.getParameter("btnSave");
                if (button != null)
                {
                    PersistenceManager pm = PMF.get().getPersistenceManager();
                    
                    Long ID;
                    ID = Long.parseLong(request.getParameter("txtID"));
                    BDoc bdoc = pm.getObjectById(BDoc.class,ID);
                    bdoc.setDocNum(request.getParameter("txtDocNum"));
                    bdoc.setReplyReceiveName(request.getParameter("txtReplyReceiveName"));
                    bdoc.setDraftUserName(request.getParameter("txtDraftUsername"));
                    bdoc.setSubject(request.getParameter("txtSubject"));
                    bdoc.setSignByName(request.getParameter("txtSignByName"));
                    bdoc.setBriefContent(request.getParameter("txtBriefContent"));
                    bdoc.setNotes(request.getParameter("txtNotes"));
                    bdoc.setDraftDate(df.parse(request.getParameter("txtDraftDate")));
                    bdoc.setSignDate(df.parse(request.getParameter("txtSignDate")));
                    bdoc.setState(Boolean.FALSE);
                    bdoc.setDocumentName(request.getParameter("fname"));

                    response.sendRedirect("VanBanNoiBo/DisplayBDoc.jsp");
                    pm.close();
                }
            }
            else if(action.equals("search"))
            {
                PersistenceManager pm = PMF.get().getPersistenceManager();
                String PubNum, Subject;
                Date FromDate, ToDate;
                
                PubNum = request.getParameter("txtPubNum");
                Subject = request.getParameter("txtSubject");
                if (request.getParameter("txtFromDate") != "" && request.getParameter("txtToDate") != "")
                {
                    FromDate = df.parse(request.getParameter("txtFromDate"));
                    ToDate = df.parse(request.getParameter("txtToDate"));
                }
                else
                {
                    FromDate = null;
                    ToDate = null;
                }

                List<BDoc> results = access.searchBDoc(PubNum, Subject, FromDate, ToDate);
                HttpSession session = request.getSession(true);
                session.setAttribute("bdoc", results);
                session.setAttribute("isSearch","y");
                response.sendRedirect("VanBanDen/DisplayBDoc.jsp");

                pm.close();
            }
            else if (action.equals("publish"))
            {
                Long BDocID ;
                PersistenceManager pm = PMF.get().getPersistenceManager();
                BDocID = Long.parseLong(request.getParameter("txtODocID"));
                BDoc bdoc = pm.getObjectById(BDoc.class,BDocID);
                bdoc.setState(Boolean.TRUE);
                response.sendRedirect("VanBanNoiBo/DisplayBDoc.jsp");
                pm.close();
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try
        {
            processRequest(request, response);
        }
        catch(ParseException ex)
        {
            Logger.getLogger(BDocServet.class.getName()).log(Level.OFF, null,ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(BDocServet.class.getName()).log(Level.OFF, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
